--[[
    local areaText = AreaText.new(text);
    areaText:setStr("wo shi zhong guo ren wo ai wo de guo jia!!!!! a");
]]

local par = cc.ui.UIScrollView;
local AreaText = class("AreaText",par);
AreaText.debug = true;
function AreaText:ctor(uiLabel)
    AreaText.super.ctor(self);
    self._target = uiLabel;

    self._bounceEnable = true;
    self:setBounceable(self._bounceEnable);

    self._direction = par.DIRECTION_VERTICAL;
    self:setDirection(self._direction);


    local size = uiLabel:getContentSize();
    self:setViewRect(cc.rect(0,0,size.width,size.height));
    local contenter = nil;
    if AreaText.debug then
        contenter = cc.LayerColor:create(cc.c4b(255,0,0,255));
    else
        contenter = cc.Layer:create();
    end
    contenter:setContentSize(cc.size(size.width,size.height));
    contenter:setTouchEnabled(false);
    self:addScrollNode(contenter);

    local anch = uiLabel:getAnchorPoint();
    local posx,posy = uiLabel:getPosition();
    self:setPosition(cc.p(posx - anch.x * size.width,posy - anch.y * size.height));
    uiLabel:getParent():addChild(self);
    uiLabel:setVisible(false);

    self:onScroll(function(event)
        if "moved" == event.name then
            self.svMoved = true;
        elseif "ended" == event.name then
            self.svMoved = false;
        end
    end)

    self.touchNode = nil;
    self.svMoved = false;
end


function AreaText:setStr(str,isRich)
    isRich = isRich or false;
    local par = self:getScrollNode();
    if not isRich then
        local conSize = par:getContentSize();
        par:removeAllChildren();
        local color = self._target:getTextColor();
        local fontName = self._target:getSystemFontName();
        local fontSize = self._target:getSystemFontSize();
        local txt = cc.ui.UILabel.new({UILabelType = 2, text = str, size = fontSize,font = fontName,align = cc.ui.TEXT_ALIGN_LEFT,valign = cc.ui.TEXT_VALIGN_TOP});
        txt:setColor(color);
        txt:setDimensions(conSize.width,0);
--        txt:setLineBreakWithoutSpace(true);
--        txt:setMaxLineWidth(conSize.width);
        txt:setAnchorPoint(cc.p(0,1));
        par:addChild(txt);
        local txtHeight = txt:getContentSize().height;
        if txtHeight >= conSize.height then
            par:setContentSize(cc.size(conSize.width,txtHeight));
        end
        local newConSize = par:getContentSize();
        txt:setPosition(cc.p(0,newConSize.height));

        self.touchNode = txt;
    else
        local RichLabel = require("richlabel.RichLabel");
        local conSize = par:getContentSize();
        local label = RichLabel.new {
            fontName = "res/my_msyh.ttf",
            fontSize = 20,
            fontColor = cc.c3b(255, 255, 255),
            maxWidth= conSize.width,
            lineSpace = 0,
            charSpace = 0,
        };
        par:addChild(label);
        label:setString(str);
        local height = label:getSize().height;
        if height >= conSize.height then
            par:setContentSize(cc.size(conSize.width,height));
        end
        local newConSize = par:getContentSize();
        label:setPosition(cc.p(0,newConSize.height));

        self.touchNode = label;
    end
    self.touchNode:setTouchSwallowEnabled(false);
    local offset = self:getViewRect().height - par:getContentSize().height;
    if offset ~= 0 then
        par:setPosition(cc.p(0,offset));
    end
    
end

function AreaText:setClickHandler(handle)
    self.touchNode:setTouchEnabled(true);
    self.touchNode:addNodeEventListener(cc.NODE_TOUCH_EVENT,function()
        if handle and not self.svMoved then
            handle();
        end
    end)
end



return AreaText;
--endregion
